unit UPharmacyStockSub;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, AdvPanel, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinOffice2010Black, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue, cxGroupBox, DBAdvGlowNavigator,
  cxStyles, dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage,
  cxNavigator, Data.DB, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid, UDMPharmacy,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, Vcl.StdCtrls, cxDBEdit, Vcl.Mask, Vcl.DBCtrls, cxCheckBox,
  cxCalendar, Vcl.Menus, cxButtons, UDMItems, UDMSettings;

type
  TFPharmacyStockSub = class(TForm)
    AdvPanel1: TAdvPanel;
    AdvPanelStyler1: TAdvPanelStyler;
    cxGroupBox1: TcxGroupBox;
    AdvPanel2: TAdvPanel;
    cxGroupBox3: TcxGroupBox;
    Label2: TLabel;
    cxDBLookupComboBox2: TcxDBLookupComboBox;
    Label3: TLabel;
    cxDBLookupComboBox3: TcxDBLookupComboBox;
    cxGroupBox2: TcxGroupBox;
    Label4: TLabel;
    cxDBTextEdit1: TcxDBTextEdit;
    Label5: TLabel;
    Label6: TLabel;
    cxDBTextEdit2: TcxDBTextEdit;
    cxDBTextEdit3: TcxDBTextEdit;
    cxGroupBox4: TcxGroupBox;
    Label7: TLabel;
    cxDBTextEdit4: TcxDBTextEdit;
    Label8: TLabel;
    cxDBTextEdit5: TcxDBTextEdit;
    Label9: TLabel;
    cxDBTextEdit6: TcxDBTextEdit;
    cxGroupBox5: TcxGroupBox;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    cxDBTextEdit7: TcxDBTextEdit;
    cxDBTextEdit8: TcxDBTextEdit;
    cxDBTextEdit9: TcxDBTextEdit;
    cxGroupBox6: TcxGroupBox;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    cxDBTextEdit10: TcxDBTextEdit;
    cxDBTextEdit11: TcxDBTextEdit;
    cxDBTextEdit12: TcxDBTextEdit;
    cxGroupBox7: TcxGroupBox;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    cxDBTextEdit13: TcxDBTextEdit;
    cxDBTextEdit14: TcxDBTextEdit;
    cxDBTextEdit15: TcxDBTextEdit;
    cxGroupBox8: TcxGroupBox;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    cxDBTextEdit16: TcxDBTextEdit;
    cxDBTextEdit17: TcxDBTextEdit;
    cxDBTextEdit18: TcxDBTextEdit;
    Label22: TLabel;
    Label23: TLabel;
    cxDBTextEdit19: TcxDBTextEdit;
    Label1: TLabel;
    cxDBDateEdit1: TcxDBDateEdit;
    DBCheckBox1: TDBCheckBox;
    Save: TcxButton;
    Label24: TLabel;
    cxLookupComboBox1: TcxLookupComboBox;
    procedure cxDBLookupComboBox2PropertiesEditValueChanged(Sender: TObject);
    procedure SaveClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure cxLookupComboBox1PropertiesEditValueChanged(Sender: TObject);
    procedure cxDBTextEdit1PropertiesEditValueChanged(Sender: TObject);
    procedure cxDBTextEdit3PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FPharmacyStockSub: TFPharmacyStockSub;
  currency_rate: integer;
  pharmaID:Integer;
  live : Boolean;
implementation

{$R *.dfm}

procedure TFPharmacyStockSub.SaveClick(Sender: TObject);
  var prevQTY : Double;
  var newQTY : Double;
begin
  pharmaID := Self.cxLookupComboBox1.EditValue;
  DMPharmacy.his_pharmacy_stock_sub.FieldByName('pharma_id').Value := pharmaID;
  DMPharmacy.his_pharmacy_stock_sub.FieldByName('currency_rate').Value := currency_rate;
  DMPharmacy.his_pharmacy_stock_sub.Post;

  DMPharmacy.his_pharmacy_stock.Locate('parmacy_stock_id',pharmaID, []);
  prevQTY := DMPharmacy.his_pharmacy_stock.FieldByName('qty_in_stock').Value;
  newQTY := DMPharmacy.his_pharmacy_stock_sub.FieldByName('qty').Value;

  DMPharmacy.his_pharmacy_stock.Edit;
  DMPharmacy.his_pharmacy_stock.FieldByName('qty_in_stock').Value := prevQTY + newQTY;
  DMPharmacy.his_pharmacy_stock.Post;

  ShowMessage('Success');
  Self.Close;
end;

procedure TFPharmacyStockSub.cxDBLookupComboBox2PropertiesEditValueChanged(
  Sender: TObject);
  var currencyID: integer;
  var currencyLlb : string;
begin
  if live then
  BEGIN
    currencyLlb := Self.cxDBLookupComboBox2.EditText;
    DMItems.his_currency.Locate('currency_label',currencyLlb, []);
    currencyID := DMItems.his_currency.FieldByName('currency_id').Value;
    currency_rate := DMItems.his_currency.FieldByName('currency_rate').Value;
    Self.Label22.Caption := 'Rate: '+IntToStr(currency_rate);
  END;
end;

procedure TFPharmacyStockSub.cxDBTextEdit1PropertiesEditValueChanged(
  Sender: TObject);
begin
  if live then
  BEGIN
    Self.cxDBTextEdit3.Enabled := True;
  END;
end;

procedure TFPharmacyStockSub.cxDBTextEdit3PropertiesEditValueChanged(
  Sender: TObject);
  var cost:Double;
  var discount:Double;
begin
  if live then
  BEGIN
    cost := Self.cxDBTextEdit1.EditingValue;
    discount := Self.cxDBTextEdit3.EditingValue;
    Self.cxDBTextEdit2.EditValue := cost + (cost * discount / 100);
    Self.cxDBTextEdit2.Text := FloatToStr(cost + (cost * discount / 100));
  END;
end;

procedure TFPharmacyStockSub.cxLookupComboBox1PropertiesEditValueChanged(
  Sender: TObject);
begin
  if live then
  BEGIN
    Self.AdvPanel2.Enabled := True;
  END;
end;

procedure TFPharmacyStockSub.FormActivate(Sender: TObject);
begin
  DMPharmacy.his_pharmacy_stock_sub.Insert;
  live := true;
end;

end.
